import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


public class Client {
    public static void main(String[] args) throws IOException {
        //创建客服端对象，连接服务器
        Socket socket = new Socket("127.0.0.1", 10000);
        System.out.println("服务器已经连接成功");

        while (true) {
            System.out.println("==============欢迎来到黑马聊天室================");
            System.out.println("1登录");
            System.out.println("2注册");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.nextLine();
            switch (choose) {
                case "1" -> login(socket);
                case "2" -> System.out.println("用户选择了注册");
                default -> System.out.println("没有这个选项");
            }
        }
    }

    private static void login(Socket socket) throws IOException {
        //向服务器写出数据
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        //键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.nextLine();
        System.out.println("请输入密码");
        String password = sc.nextLine();
        //拼接
        StringBuilder sb = new StringBuilder();
        //username=zhangsan&password=123
        sb.append("username=").append(username).append("&password=").append(password);
        //第一次写的是执行登录操作
        method(bw, "login");

        //第二次写的是用户名和密码的信息
        //往服务器写出用户名和密码
        method(bw, sb.toString());

        //接受服务器反馈的信息
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String message = br.readLine();
        //状态码 1：登录成功 2：用户名不存在 3：密码错误
        if ("1".equals(message)) {
            System.out.println("登录成功,开始聊天");

            //开始聊天的时候，服务器可能会向客服端发信息
            //开一条单独的线程，专门负责接受服务器发来的信息
            ClientThread t = new ClientThread(socket);
            t.start();
            //开始聊天
            talkAll2(bw);
        } else if ("3".equals(message)) {
            System.out.println("用户名不存在");
        } else if ("2".equals(message)) {
            System.out.println("密码错误");
        }
    }

    //向服务器写出消息
    private static void talkAll2(BufferedWriter bw) throws IOException {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入消息：");
            String str = sc.nextLine();
            ;
            method(bw, str);
        }
    }

    private static void method(BufferedWriter bw, String str) throws IOException {
        bw.write(str);
        bw.newLine();
        bw.flush();
    }
}

//接受服务器发来的群发消息
class ClientThread extends Thread {
    Socket socket;

    ClientThread(Socket socket) {
        this.socket = socket;
    }


    //接受服务器发来的消息
    @Override
    public void run() {
       // 循环重复的接受
        while (true) {
            try {
                //接受服务器发来的聊天记录
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String message = br.readLine();
                System.out.println(message);

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}



